//
//  ZLAttributedStringExtension.swift
//  ZLExtension
//
//  Created by 科大 on 2024/12/16.
//

import UIKit

public enum ZLAttributeMode {
    case all
    case first
}

public extension NSMutableAttributedString {
    func zl_addAttribute(_ string: String, font: UIFont? = nil, color: UIColor? = nil) {
        var attributes: [NSAttributedString.Key: Any] = [:]
        if font != nil {
            attributes[.font] = font
        }
        if font != nil {
            attributes[.foregroundColor] = color
        }
        self.append(NSAttributedString(string: string, attributes: attributes))
    }
}

public extension NSAttributedString {
    func zl_width(height: CGFloat) -> Double {
        let constraintSize = CGSize(width: CGFloat.greatestFiniteMagnitude, height: height)
        let boundingBox = self.boundingRect(
            with: constraintSize,
            options: [.usesLineFragmentOrigin, .usesFontLeading],
            context: nil
        )

        return ceil(boundingBox.width)
    }
}
